//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.qy137.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qy137.entity.Article;
import com.qy137.entity.BottomArticle;
import com.qy137.vo.BottomArticleVo;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.qy137.dao.ArticleDao;
import org.qy137.dao.BottomArticleDao;
import org.qy137.service.BottomArticleService;
import org.qy137.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BottomArticleServiceImpl implements BottomArticleService {
    @Autowired
    private BottomArticleDao bottomArticleDao;
    @Autowired
    private ArticleDao articleDao;

    public BottomArticleServiceImpl() {
    }

    public CommonResult getAllBottomArticle(Integer page, Integer limit, BottomArticleVo bottomArticleVo) {
        Page<BottomArticle> page1 = new Page((long)page, (long)limit);
        QueryWrapper<BottomArticle> wrapper = new QueryWrapper();
        wrapper.eq("is_deleted", 0);
        wrapper.eq("parent_id", "0");
        if (StringUtils.isNotEmpty(bottomArticleVo.getName())) {
            wrapper.like("name", bottomArticleVo.getName());
        }

        if (bottomArticleVo.getIsDisable() != null) {
            wrapper.eq("is_disable", bottomArticleVo.getIsDisable());
        }

        Page<BottomArticle> bottomArticlePage = (Page)this.bottomArticleDao.selectPage(page1, wrapper);
        List<BottomArticle> records = bottomArticlePage.getRecords();
        QueryWrapper<BottomArticle> wrapperChildren = new QueryWrapper();
        wrapperChildren.eq("is_deleted", 0);
        wrapperChildren.notIn("parent_id", new Object[]{"0"});
        List<BottomArticle> children = this.bottomArticleDao.selectList(wrapperChildren);
        System.out.println(children);
        Iterator var10 = records.iterator();

        while(var10.hasNext()) {
            BottomArticle record = (BottomArticle)var10.next();
            ArrayList<BottomArticle> bottomArticles = new ArrayList();
            Iterator var13 = children.iterator();

            while(var13.hasNext()) {
                BottomArticle child = (BottomArticle)var13.next();
                if (child.getParentId().equals(record.getId())) {
                    bottomArticles.add(child);
                }
            }

            record.setChildren(bottomArticles);
        }

        return new CommonResult(2000, "查询成功", bottomArticlePage);
    }

    public CommonResult changeStatus(String id, Integer isDisable) {
        BottomArticle bottomArticle = new BottomArticle();
        QueryWrapper<BottomArticle> wrapper = new QueryWrapper();
        wrapper.eq("id", id);
        BottomArticle bottomArticle1 = (BottomArticle)this.bottomArticleDao.selectOne(wrapper);
        if ("0".equals(bottomArticle1.getParentId())) {
            QueryWrapper<BottomArticle> wrapper1 = new QueryWrapper();
            wrapper1.eq("parent_id", id);
            wrapper1.or();
            wrapper1.eq("id", id);
            bottomArticle.setIsDisable(isDisable);
            this.bottomArticleDao.update(bottomArticle, wrapper1);
            return new CommonResult(2000, "修改成功", (Object)null);
        } else {
            bottomArticle.setId(id);
            bottomArticle.setIsDisable(isDisable);
            int i = this.bottomArticleDao.updateById(bottomArticle);
            return i > 0 ? new CommonResult(2000, "修改成功", (Object)null) : new CommonResult(5000, "修改失败", (Object)null);
        }
    }

    public CommonResult deleteBottomArticle(String id) {
        BottomArticle bottomArticle = (BottomArticle)this.bottomArticleDao.selectById(id);
        BottomArticle bottomArticle2 = new BottomArticle();
        bottomArticle2.setIsDeleted(1);
        int i;
        if ("0".equals(bottomArticle.getParentId())) {
            QueryWrapper<BottomArticle> wrapper = new QueryWrapper();
            wrapper.eq("id", id);
            wrapper.or();
            wrapper.eq("parent_id", id);
            i = this.bottomArticleDao.update(bottomArticle2, wrapper);
            return i > 0 ? new CommonResult(2000, "操作成功", (Object)null) : new CommonResult(5000, "操作失败", (Object)null);
        } else {
            BottomArticle bottomArticle1 = new BottomArticle();
            bottomArticle1.setId(id);
            bottomArticle1.setIsDeleted(1);
            i = this.bottomArticleDao.updateById(bottomArticle1);
            return i > 0 ? new CommonResult(2000, "操作成功", (Object)null) : new CommonResult(5000, "操作失败", (Object)null);
        }
    }

    public CommonResult updateBottomArticle(BottomArticleVo bottomArticleVo) {
        QueryWrapper<BottomArticle> wrapper = new QueryWrapper();
        wrapper.eq("name", bottomArticleVo.getName());
        wrapper.eq("is_deleted", 0);
        BottomArticle bottomArticle = (BottomArticle)this.bottomArticleDao.selectOne(wrapper);
        if (bottomArticle != null) {
            return new CommonResult(2000, "名字已经存在，换个名字试试", (Object)null);
        } else {
            BottomArticle bottomArticle1 = new BottomArticle();
            bottomArticle1.setId(bottomArticleVo.getId());
            bottomArticle1.setName(bottomArticleVo.getName());
            bottomArticle1.setGmtModified(LocalDateTime.now());
            int i = this.bottomArticleDao.updateById(bottomArticle1);
            return i > 0 ? new CommonResult(2000, "修改成功", (Object)null) : new CommonResult(5000, "修改失败", (Object)null);
        }
    }

    public CommonResult addBottomArticle(BottomArticleVo bottomArticleVo) {
        QueryWrapper<BottomArticle> wrapper = new QueryWrapper();
        wrapper.eq("name", bottomArticleVo.getName());
        wrapper.eq("is_deleted", 0);
        BottomArticle one = (BottomArticle)this.bottomArticleDao.selectOne(wrapper);
        if (one != null) {
            return new CommonResult(5000, "名字已经存在", (Object)null);
        } else {
            BottomArticle article = new BottomArticle();
            article.setName(bottomArticleVo.getName());
            article.setIsDeleted(0);
            article.setIsDisable(0);
            article.setGmtModified(LocalDateTime.now());
            article.setGmtCreate(LocalDateTime.now());
            article.setParentId(bottomArticleVo.getParentId());
            int insert = this.bottomArticleDao.insert(article);
            if (insert > 0 & !"0".equals(bottomArticleVo.getParentId())) {
                QueryWrapper<BottomArticle> queryWrapper = new QueryWrapper();
                queryWrapper.eq("name", bottomArticleVo.getName());
                String id = ((BottomArticle)this.bottomArticleDao.selectOne(queryWrapper)).getId();
                Article article1 = new Article();
                article1.setGmtCreate(LocalDateTime.now());
                article1.setGmtModified(LocalDateTime.now());
                article1.setIsDeleted(0);
                article1.setNavId(id);
                article1.setArtTitle(bottomArticleVo.getName());
                int insert1 = this.articleDao.insert(article1);
                if (insert1 > 0) {
                    return new CommonResult(2000, "添加成功", (Object)null);
                }
            }

            return insert > 0 ? new CommonResult(2000, "添加成功", (Object)null) : new CommonResult(5000, "添加失败", (Object)null);
        }
    }
}
